package com.bleeth.fx.component;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.bleeth.fx.helper.StyleHelper;
import com.bleeth.fx.layout.FormBuilder;
import com.bleeth.fx.layout.GirdCell;
import javafx.geometry.Pos;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import lombok.Builder;

/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@Builder
public class MIconPasswordField {

    @Builder.Default
    private String icon = "icon-key";


    @Builder.Default
    private double width = 300;

    @Builder.Default
    private double height = 40;

    @Builder.Default
    private String placeholder = "";


    public Region make() {
        Region leftIcon = MIcon.builder().icon(icon).size(20).build().make();
        TextField textField = new TextField();
        PasswordField passwordField = new PasswordField();
        Region viewIcon = MIcon.builder().icon("icon-view").size(20).build().make();
        VBox box = FormBuilder.form(width, height, Pos.CENTER)
                .coverRow(GirdCell.createCell(30D, Pos.CENTER), GirdCell.createCell(1, Pos.CENTER),
                        GirdCell.createCell(30D, Pos.CENTER))
                .addCell(0, leftIcon)
                .coverCell(1, passwordField)
                .addCell(2, viewIcon)
                .endRow(false)
                .build("", "MIconPasswordField");

        //box设置边框
        StyleHelper.br(box).radius(5).width(1).color("#1f202488").style("SOLID").make();

        //textField 移除边框
        viewIcon.setVisible(false);
        textField.setStyle("-fx-background-color: #f4f4f4");
        passwordField.setStyle("-fx-background-color: #f4f4f4");

        textField.setPromptText(placeholder);
        passwordField.setPromptText(placeholder);

        FormBuilder.RowBuilder rowBuilder = FormBuilder.queryForm("MIconPasswordField").queryRowByCell(passwordField);

        //添加cleanIcon操作事件
        viewIcon.setOnMouseClicked(event -> {
            rowBuilder.clearCell(1);
            rowBuilder.coverCell(1, textField);
        });

        textField.textProperty().addListener((observable, oldValue, newValue) -> {
            if (StrUtil.isEmpty(newValue)) {
                viewIcon.setVisible(false);
                return;
            }
            if (StrUtil.isEmpty(oldValue) && StrUtil.isNotEmpty(newValue)) {
                viewIcon.setVisible(true);
            }
        });

        textField.focusedProperty().addListener((observable, oldValue, newValue) -> {
            if (textField.isFocused()) {
                StyleHelper.br(box).radius(5).width(1).color("#409eff").style("SOLID").make();
            } else {
                StyleHelper.br(box).radius(5).width(1).color("#1f202488").style("SOLID").make();
            }
        });
        return box;
    }

    public static void wrapperPassword(TextField textField) {
        String text = textField.getText();
        StrBuilder strBuilder = new StrBuilder();
        for (int i = 0; i < text.length(); i++) {
            strBuilder.append(".");
        }

    }
}
